package net.tv90.bfmccore.command;

import net.tv90.bfmccore.helper.FlanHelper;
import net.tv90.bfmccore.helper.InventoryHelper;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.List;

import static net.tv90.bfmccore.helper.ItemHelper.stringToMaterial;


public class ItemCommand implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("inst")) {

            if (args.length == 0) {
                sender.sendMessage("/inst hand <配件类型> <配件英文id> 为手中枪械安装配件");
                sender.sendMessage("/inst gun <枪械id> <配件类型> <配件英文id> 为背包中枪械安装配件");
                sender.sendMessage("/inst ammo <弹药英文id> 为手中枪械安装弹药");
                sender.sendMessage("/inst find 查询手中枪械的配件");
                sender.sendMessage("/inst info 查看手中物品的信息");
            }
            else if (args[0].equalsIgnoreCase("hand")) {
                Player p = (Player) sender;
                Inventory inv = p.getInventory();
                ItemStack oldGun = p.getInventory().getItemInHand();
                String type = args[1];
                Material attachment = stringToMaterial(args[2]);
                ItemStack newGun = FlanHelper.setAttachment(oldGun, type, attachment);
                inv.remove(oldGun);
                inv.addItem(newGun);
            }
            else if (args[0].equalsIgnoreCase("gun")) {
                Player p = (Player) sender;
                Inventory inv = p.getInventory();
                Material gun = stringToMaterial(args[1]);
                ItemStack isk = InventoryHelper.hasItemToStack(inv, gun);
                String type = args[2];
                Material attachment = stringToMaterial(args[3]);
                if (!(isk.getType().name().equals("AIR"))) {
                    ItemStack newGun = FlanHelper.setAttachment(isk, type, attachment);
                    inv.remove(isk);
                    inv.addItem(newGun);
                } else {
                    sender.sendMessage("背包中没有" + gun);
                }
            }
            else if (args[0].equalsIgnoreCase("info")) {
                Player p = (Player) sender;
                ItemStack itemStack = p.getInventory().getItemInHand();
                String name = itemStack.getType().name();
                int id = itemStack.getTypeId();
                sender.sendMessage("手中物品的英文ID为" + name + ",数字ID为" + id);
            }
            else if (args[0].equalsIgnoreCase("ammo")) {
                Player p = (Player) sender;
                Inventory inv = p.getInventory();
                ItemStack oldGun = p.getInventory().getItemInHand();
                Material ammo = stringToMaterial(args[1]);
                ItemStack newGun = FlanHelper.setAmmo(oldGun,ammo);
                inv.remove(oldGun);
                inv.addItem(newGun);
            }
            else if (args[0].equalsIgnoreCase("find")) {
                Player p = (Player) sender;
                ItemStack gun = p.getInventory().getItemInHand();
                List<String> list = FlanHelper.getAllAttachment(gun);
                p.sendMessage(list.toString());
            }
            return true;
        }
        return true;
    }
}
